/*
** constructors.c for  in /home/lucas/projet/philosophe/laidfilauxzauphe
** 
** Made by Lucas Stefas
** Login   <stefas_l@epitech.net>
** 
** Started on  Sat Apr 30 10:05:36 2011 Lucas Stefas
** Last update Sat Apr 30 10:05:37 2011 Lucas Stefas
*/

#include <unistd.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <pthread.h>
#include "plsp.h"

philosophe*	new_philosophe(size_t id, table *table)
{
  philosophe*	philosophes = malloc(sizeof(*philosophes));

  if (!philosophes)
    {
      fprintf(stderr, "Error: %s\n", strerror(errno));
      exit(EXIT_FAILURE);
    }
  philosophes->table = table;
  philosophes->id = id;
  philosophes->left_hand = NULL;
  philosophes->right_hand = NULL;
  philosophes->think = think;
  philosophes->eat = eat;
  philosophes->sleep = dodo;
  philosophes->remove_baguette = remove_baguette;
  philosophes->add_baguette = add_baguette;
  philosophes->look_for_a_baguette = look_for_a_baguette;
  return (philosophes);
}

baguette*	new_baguette(size_t id)
{
  baguette*	baguettes = malloc(sizeof(*baguettes));

  if (!baguettes)
    {
      fprintf(stderr, "Error: %s\n", strerror(errno));
      exit(EXIT_FAILURE);
    }
  baguettes->id = id;
  pthread_mutex_init(&baguettes->mutex, NULL);
  baguettes->is_used = 0;
  baguettes->be_used = be_used;
  baguettes->free = free_baguette;
  return (baguettes);
}

table*		new_table(baguette** baguettes, size_t nb_baguettes)
{
  table*	tables = NULL;

  tables = malloc(sizeof(*tables));
  if (!tables)
    {
      fprintf(stderr, "Error: %s\n", strerror(errno));
      exit(EXIT_FAILURE);
    }
  tables->nb_baguettes = nb_baguettes;
  tables->baguettes = baguettes;
  pthread_mutex_init(&tables->starter, NULL);
  pthread_mutex_init(&tables->looker, NULL);
  return (tables);
}
